﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using AlexPilotti.FTPS.Client;
using AlexPilotti.FTPS.Common;
using System.IO;

namespace YLPublicService.Common
{
    public class FtpHelper
    {
        private string host;
        private NetworkCredential credential;
        private FTPSClient client;
        public FtpHelper(int[] ip, string name, string pwd)
        {
            this.host = getHost(ip);
            credential = new NetworkCredential(name, pwd);
        }
        private string getHost(int[] ip)
        {
            return host = ip.Select(i => i.ToString()).Aggregate((acc, str) => acc += "." + str);
        }
        public FtpHelper(string host, string name, string pwd)
        {
            this.host = host;
            credential = new NetworkCredential(name, pwd);
        }
        /// <summary>
        /// 模板目录约定Templates 
        /// </summary>
        /// <returns></returns>
        public IEnumerable<string> GetTemplateNames()
        {
            using (client = new FTPSClient())
            {
                client.Connect(host, credential, ESSLSupportMode.ClearText);
                client.SetCurrentDirectory("Templates");
                var names = client.GetDirectoryList();
                return names.Select(item => item.Name);
            }
        }
        public void DownloadFile(string remoteFileName, string outputFileName)
        {
            client = new FTPSClient();
            client.Connect(host, credential, ESSLSupportMode.ClearText);
            var fs = client.GetFile(remoteFileName);
            saveFile(fs, outputFileName);
            fs.Close();
            client.Close();
        }
        public void UploadFile(string remoteFileName, string localFileName)
        {
            client = new FTPSClient();
            client.Connect(host, credential, ESSLSupportMode.ClearText);
            client.PutFile(localFileName, remoteFileName);
            client.Close();
        }
        public bool TyUploadFile(string rFn, string lFn)
        {
            try
            {
                UploadFile(rFn, lFn);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void saveFile(FTPStream input, string outputFileName)
        {
            FileStream fs;
            if (File.Exists(outputFileName) == false)
            {
                fs = File.Create(outputFileName);

            }
            else
            {
                fs = File.OpenWrite(outputFileName);
            }
            byte[] buff = new byte[1024];
            int num = input.Read(buff, 0, buff.Length);
            while (num != 0)
            {
                fs.Write(buff, 0, num);
                num = input.Read(buff, 0, buff.Length);
            }
            input.Close();
            fs.Close();
        }
    }
}